package run;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import com.src.calendarItem.Event;
import com.src.calendarItem.Event.EventClass;



public class dbConnection {

	private Connection conn;
	private HashMap<Integer,Event> allevents;
	
	public dbConnection()
	{
		if(connectdb())
		{
			System.out.println("Database Connected!");
		}
	}
	
	private boolean connectdb()
	{
		String dataSourceName = "calendar";
		String dbURL = "jdbc:odbc:" + dataSourceName;
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			conn = DriverManager.getConnection(dbURL,"","");			
			return true;
		}
		catch(Exception error)
		{
			error.printStackTrace();
		}
		
		return false;
	}
	
	public Event getEvent(int id)
	{
		Event event = null;
		
		if(conn==null)
			connectdb();
		
		try 
		{
				Statement stmt = conn.createStatement();
				
		} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		
		return event;
	}
	
	public boolean saveEvent(Event ev)
	{
		return saveEvent(ev.getSummary(),ev.getCategory(),ev.getStartDate(),ev.getStartTime(),ev.getEndDate(),ev.getEndTime(),ev.getLocation(),ev.getDescription(),ev.getEventClass());
	}
	
	public boolean saveEvent(String sum, String cate, String stDt,String stTm, String endDt,String endTm, String location, String desc, Event.EventClass cla)
	{
		boolean save = false;
		if(conn==null)
			connectdb();
		try
		{
			Statement stmt= conn.createStatement();
			stmt.execute("INSERT INTO Event (SUMMARY,CATEGORY,STARTDATE,STARTTIME,ENDDATE,ENDTIME,LOCATION,DESCRIPTION,CLASS) VALUES('"+sum+"','"+cate+"','"+stDt+"','"+stTm+"','"+endDt+"','"+endTm+"','"+location+"','"+desc+"','"+cla.toString()+"')");
			return save = true;
		}
		catch(Exception error)
		{
			error.printStackTrace();
		}
		
		return save;
		
	}
	
	public boolean updateEvent(Event ev)
	{
		if(conn == null)
			connectdb();
		try
		{
			PreparedStatement updateevent = conn.prepareStatement("UPDATE Event  SET SUMMARY = '"+ev.getSummary()+"', CATEGORY = '"+ev.getCategory()+"', STARTDATE = " +
			"'"+ev.getStartDate()+"', STARTTIME = '"+ev.getStartTime()+"',ENDDATE = '"+ev.getEndDate()+"', ENDTIME = '"+ev.getEndTime()+"',LOCATION = '"+ev.getLocation()+"'," +
			"DESCRIPTION = '"+ev.getDescription()+"',CLASS = '"+ev.getEventClass()+"' WHERE id = ?");
			updateevent.setInt(1, ev.getID());
			
			updateevent.executeUpdate();
			
			//stmt.execute("UPDATE Event SET summary = '"+ev.getSummary()+"' WHERE id = 'Integer.valueOf(ev.getID())'");
			return true;
		}catch(Exception error)
		{
			error.printStackTrace();
		}
		
		return false;
	}
	
	public boolean deleteEvent(int evid)
	{
		if(conn == null)
			connectdb();
		try
		{
			PreparedStatement deleteEvent = conn.prepareStatement("DELETE FROM EVENT WHERE id= ?");
			deleteEvent.setInt(1,evid);
			deleteEvent.execute();
			return true;
		}catch(Exception error)
		{
			error.printStackTrace();
		}
		
		return false;
	}
	
	public HashMap<Integer,Event> getAllEvents()
	{
		allevents = new HashMap<Integer,Event>();
		
		//select all events and put in HashMap, Key is the Event ID
		
		if(conn==null)
		{
			connectdb();
		}
		try
		{
			ResultSet rs = null;
			Statement stmt = conn.createStatement();
			rs = stmt.executeQuery("SELECT * FROM Event");
			
			while(rs.next())
			{
				int eventID = rs.getInt("ID");
				String sum = rs.getString("SUMMARY");
				String eventCate = rs.getString("CATEGORY");
				String stDt = rs.getString("STARTDATE");
				String stTm = rs.getString("STARTTIME");
				String endDt = rs.getString("ENDDATE");
				String endTm = rs.getString("ENDTIME");
				String loc = rs.getString("LOCATION");
				String desc = rs.getString("DESCRIPTION");
				EventClass cls = EventClass.valueOf(rs.getString("CLASS"));
				
				Event eve = new Event(eventID,sum,eventCate,stDt,stTm,endDt,endTm,desc,loc,cls);

				allevents.put(eventID,eve);
				
			} 
		}catch(Exception error)
		{
			error.printStackTrace();
		}
		
		return allevents;
	}
	
	public Vector<Event> getEventofDate(String stDt)
	{
		Vector<Event> events = new Vector<Event>();
		
		if(allevents==null)
		{
			getAllEvents();
		}
		
		for(Map.Entry<Integer, Event> entry:allevents.entrySet() )
		{
			Event event = (Event)entry.getValue();
			if(event.getStartDate().equals(stDt))
			{
				events.add(event);
			}
		}
		
		return events;
	}
	
	
	
	public boolean hasEvent(String stDt)
	{
		if(!getEventofDate(stDt).isEmpty())
		{
			return true;
		}
		
		return false;
	}
}
